/**
 * 
 */
package org.starsource.forum.server.transaction.impl;

import java.sql.Timestamp;

import org.starsource.forum.client.exception.DatabaseException;
import org.starsource.forum.client.exception.LoginLogNotFoundException;
import org.starsource.forum.client.exception.UserNotFoundException;
import org.starsource.forum.server.domain.LoginLog;
import org.starsource.forum.server.domain.User;
import org.starsource.forum.server.dao.UserDao;
import org.starsource.forum.server.dao.LoginLogDao;
import org.starsource.forum.server.transaction.UserTransaction;

/**
 * @author james
 *
 */
public class UserTransactionImpl implements UserTransaction {

	private UserDao userDao;
	private LoginLogDao loginLogDao;
	
	public void setLoginLogDao(LoginLogDao loginLogDao) {
		this.loginLogDao = loginLogDao;
	}
	
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
	/* (non-Javadoc)
	 * @see org.starsource.forum.server.service.UserService#deleteUser(org.starsource.forum.server.domain.User)
	 */
	@Override
	public void removeUser(User user) throws DatabaseException {
		userDao.delete(user);

	}

	/* (non-Javadoc)
	 * @see org.starsource.forum.server.service.UserService#getLastVisit(org.starsource.forum.server.domain.User)
	 */
	@Override
	public LoginLog getLastVisit(User user) throws LoginLogNotFoundException, DatabaseException {
		return loginLogDao.getLastLoginLog(user);
	}

	/* (non-Javadoc)
	 * @see org.starsource.forum.server.service.UserService#getUser(int)
	 */
	@Override
	public User getUser(int id) throws UserNotFoundException, DatabaseException {
		return userDao.getUser(id);
	}

	/* (non-Javadoc)
	 * @see org.starsource.forum.server.service.UserService#getUser(java.lang.String, java.lang.String)
	 */
	@Override
	public User getUser(String account, String password) throws UserNotFoundException, DatabaseException {
		return userDao.getUser(account, password);
	}

	/* (non-Javadoc)
	 * @see org.starsource.forum.server.service.UserService#loginSuccess(org.starsource.forum.server.domain.User, java.lang.String)
	 */
	@Override
	public void loginSuccess(User user, String ip) throws DatabaseException {
		LoginLog loginLog = new LoginLog();
		loginLog.setUser(user);
		loginLog.setIp(ip);
		loginLog.setTimestamp(new Timestamp(new java.util.Date().getTime()/1000*1000));
        loginLogDao.save(loginLog);
	}

	/* (non-Javadoc)
	 * @see org.starsource.forum.server.service.UserService#saveUser(org.starsource.forum.server.domain.User)
	 */
	@Override
	public User createUser(String account, String password, String name, String directory) throws DatabaseException {
		User user = new User(account, password, name, directory);
		userDao.save(user);
		return user;
	}

	/* (non-Javadoc)
	 * @see org.starsource.forum.server.service.UserService#updateUser(org.starsource.forum.server.domain.User)
	 */
	@Override
	public void updateUser(User user) throws DatabaseException {
		userDao.update(user);
	}

	@Override
	public User getUser(String account) throws UserNotFoundException,
			DatabaseException {
		return userDao.getUser(account);
	}

}
